//
//  KnowledgeController.m
//  Water
//
//  Created by yh on 2017/12/19.
//  Copyright © 2017年 chenguibang. All rights reserved.
//

#import "KnowledgeController.h"
#import "KnowledgeTextCell.h"
#import "KnowledgeOneImageCell.h"
@interface KnowledgeController ()<UITableViewDelegate,UITableViewDataSource,KnowledgeCellHandleDelegate>{
    NSArray *data;
}

@end

@implementation KnowledgeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupView];
    
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"knows" ofType:@"plist"];
    
    
    data = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
    [self.tableView reloadData];
    
}


- (void)setupView{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[KnowledgeOneImageCell class] forCellReuseIdentifier:@"KnowledgeOneImageCell"];
     [self.tableView registerClass:[KnowledgeTextCell class] forCellReuseIdentifier:@"KnowledgeTextCell"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return data.count;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    KnowledgeCell *cell = [tableView dequeueReusableCellWithIdentifier:indexPath.row % 2 == 0 ? @"KnowledgeTextCell" : @"KnowledgeOneImageCell" forIndexPath:indexPath];
    cell.delegate = self;
    [cell setDataModel:data[indexPath.row]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [tableView fd_heightForCellWithIdentifier:indexPath.row % 2 == 0 ? @"KnowledgeTextCell" : @"KnowledgeOneImageCell" configuration:^(KnowledgeTextCell* cell) {
        cell.delegate = self;
       [cell setDataModel:data[indexPath.row]];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)reload{
    [self.tableView reloadData];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
